<template>
	<view>
		<uni-nav-bar :statusBar="true" dark title="创建赛季" left-icon="left" fixed @clickLeft="clickLeft">

		</uni-nav-bar>
		<view class="content">
			<uni-forms class="form" ref="baseForm" :modelValue="baseFormData" :rules="customRules">
				<uni-forms-item labelWidth="200rpx" label="赛季名称" required name="seasonName">
					<uni-easyinput v-model="baseFormData.seasonName" placeholder="请输赛季名称" />
				</uni-forms-item>
				<uni-forms-item labelWidth="200rpx" label="赛季开始时间" required name="seasonBeginTime">
					<uni-datetime-picker v-model="baseFormData.seasonBeginTime" type="date" :clear-icon="false"
						@maskClick="maskClick">
						<uni-easyinput    v-model="baseFormData.seasonBeginTime" placeholder="请选择赛季开始时间" />
					</uni-datetime-picker>
				</uni-forms-item>
				<uni-forms-item labelWidth="200rpx" label="赛季结束时间" required name="seasonEndTime">
					<uni-datetime-picker v-model="baseFormData.seasonEndTime" type="date" :clear-icon="false"
						@maskClick="maskClick">
						<uni-easyinput v-model="baseFormData.seasonEndTime" placeholder="请选择赛季结束时间" />
					</uni-datetime-picker>
				</uni-forms-item>
			</uni-forms>
		</view>

		<view class="bottomBtn">
			<button @click="onCreated" :loading="disabled" :disabled="disabled">
				提交
			</button>
		</view>
	</view>
</template>

<script>
	import {createdseason} from '@/api/season/indx.js'
	export default {
		data() {
			return {
				disabled: false,
				baseFormData: {
					seasonName: '',
					seasonBeginTime: '',
					seasonEndTime: ''
				},
				customRules: {
					seasonName: {
						rules: [{
							required: true,
							errorMessage: '请输入赛季名称'
						}]
					},
					seasonBeginTime: {
						rules: [{
							required: true,
							errorMessage: '请选择赛季开始时间'
						},
						{
							validateFunction: function(rule, value, data, callback) {
								console.log(value, data)
								if (data.seasonEndTime && (new Date(data.seasonEndTime).getTime() <=
										new Date(value).getTime())) {
									callback('开始时间不能大于结束时间')
								}
								return true
							}
						}]
					},
					seasonEndTime: {
						rules: [{
							required: true,
							errorMessage: '请选择赛季结束时间'
						},
						{
							validateFunction: function(rule, value, data, callback) {
								console.log(value, data)
								if (data.seasonBeginTime && (new Date(data.seasonBeginTime).getTime() >=
										new Date(value).getTime())) {
									callback('结束时间不能小于开始时间')
								}
								return true
							}
						}]
					}
				}
			}
		},
		onReady() {
			this.$refs.baseForm.setRules(this.customRules)
		},
		methods: {
			clickLeft() {
				uni.navigateBack()
			},
			onCreated() {
				this.disabled = true;
				this.baseFormData.organizationId =  uni.getStorageSync('scoringWay');
				this.baseFormData.contestId = uni.getStorageSync('contestId');
				this.$refs['baseForm'].validate().then(res => {
					console.log('success', res);
					createdseason(this.baseFormData).then(data => {
						if (data.code == 200) {
							this.disabled = false;
							this.$modal.msg("创建成功")
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/home/home'
								})
							}, 400)
						} else {
							this.disabled = false;
						}
					}).catch(err => {
						this.disabled = false;
					})
				}).catch(err => {
					this.disabled = false;
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		padding: 30rpx;
	}

	.bottomBtn {
		position: absolute;
		bottom: 0;
		width: 100%;
		padding: 30rpx;
		padding-bottom: calc(40rpx + env(safe-area-inset-bottom));

		button {
			background-color: $btn-bg-color;
			color: #fff;
		}
	}
</style>